package com.sukaiyi.giteeideaplugin.utils;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.ui.EditorTextField;
import com.intellij.util.ui.AnimatedIcon;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;

public class IdeaUIUtils {

    private static final Icon[] LOADING_ICONS = new Icon[]{
            AllIcons.Process.Big.Step_1,
            AllIcons.Process.Big.Step_2,
            AllIcons.Process.Big.Step_3,
            AllIcons.Process.Big.Step_4,
            AllIcons.Process.Big.Step_5,
            AllIcons.Process.Big.Step_6,
            AllIcons.Process.Big.Step_7,
            AllIcons.Process.Big.Step_8,
    };
    private static final Icon PASSIVE_ICON = AllIcons.Process.Big.Step_passive;
    public static final int CYCLE_LENGTH = com.intellij.ui.AnimatedIcon.Default.DELAY * LOADING_ICONS.length;

    public static AnimatedIcon createBigProgressIcon() {
        return new AnimatedIcon("loading", LOADING_ICONS, PASSIVE_ICON, CYCLE_LENGTH);
    }

    public static EditorTextField createEditorTextField(Project project, FileType fileType) {
        return createEditorTextField(project, false, fileType);
    }

    public static EditorTextField createEditorTextField(Project project, boolean isViewer, FileType fileType) {
        return new EditorTextField(null, project, fileType, isViewer, false) {
            @Override
            protected boolean shouldHaveBorder() {
                return StringUtils.isNotEmpty(getText());
            }

            @Override
            protected EditorEx createEditor() {
                EditorEx editor = super.createEditor();
                final EditorSettings settings = editor.getSettings();
                settings.setFoldingOutlineShown(true);
                settings.setLineNumbersShown(true);
                settings.setWheelFontChangeEnabled(true);
                editor.setShowPlaceholderWhenFocused(true);
                editor.setVerticalScrollbarVisible(true);
                editor.setHorizontalScrollbarVisible(true);
                editor.setFontSize(14);
                return editor;
            }
        };
    }
}
